﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace MillerApp.CommonLib.OfficeProvider
{
    /// <summary>
    /// Word操作服务类
    /// </summary>
    public class WordProvider:IOfficeProvider
    {
        //1.通过模板+XML数据生成 新的文件
        //public void
 
        //2. 读取Word的内容

        //3.

        WordUtility wordUtility;

        public WordProvider()
        {
            wordUtility = new WordUtility();
        }

        /// <summary>
        /// 根据文件模板及数据内容生成文件
        /// </summary>
        /// <param name="templateFilePath">模板文件路径</param>
        /// <param name="xml">XML数据内容</param>
        /// <returns></returns>
        public  Stream Generate(string templateFilePath, XDocument xml)
        {
            Stream s = System.IO.File.OpenRead(templateFilePath);
            if (s == null)
            {
                throw new Exception("文件未找到或未能打开,文件为"+templateFilePath);
            }
            return Generate(s, xml);
        }
        /// <summary>
        /// 根据模板文件流及数据内容生成文件
        /// </summary>
        /// <param name="templateFileStream">模板文件流</param>
        /// <param name="xml">XML数据内容</param>
        /// <returns></returns>
        public  Stream Generate(Stream templateFileStream, XDocument xml)
        {
            //如果模板文件为空文件的时候，则返回NULL
            if (templateFileStream.Length == 0)
            {
                return null;
            }
            MemoryStream ms = new MemoryStream();
            templateFileStream.CopyTo(ms);
            templateFileStream.Close();
            ms = this.wordUtility.ParseAndFill(ms, xml);
            ms.Position = 0;
            return ms;
        }

        public  Stream GenerateWatermark(Stream wordFileStream, Stream watermarkFileStream)
        {
            return null;
        }

    }

    public class WordUtility
    {
        public MemoryStream ParseAndFill(MemoryStream templateFileStream, XDocument xml)
        {
            //using(WordProvider
            return null;
        }
    }
}
